package com.xn.app.muying.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by xinnuo on 2016/9/26.
 */

public class HttpUtil {
    private static ExecutorService service = Executors.newCachedThreadPool();

    //下载文字
    public static void getData(final String path, final Handler handler,
                               final OnGetDataListener listener) {
        service.execute(new Runnable() {

            @Override
            public void run() {
                InputStream is = null;
                ByteArrayOutputStream os = null;
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.connect();
                    if (conn.getResponseCode() == 200) {
                        is = conn.getInputStream();
                        os = new ByteArrayOutputStream();
                        byte[] b = new byte[1024];
                        int len = 0;
                        while ((len = is.read(b)) != -1) {
                            os.write(b, 0, len);
                        }
                        os.flush();
                        byte[] array = os.toByteArray();
                        final String data = new String(array, 0, array.length);
                        handler.post(new Runnable() {

                            @Override
                            public void run() {
                                listener.onJsonData(data);
                            }
                        });
                    }
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    if (os != null) {
                        try {
                            os.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }

    public interface OnGetDataListener {
        public void onJsonData(String data);
    }

    //下载图片
    public static void getBitMap(final String path, final Handler handler,
                                 final ImageView iv) {

        service.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.connect();
                    if (conn.getResponseCode() == 200) {
                        final Bitmap bitmap = BitmapFactory.decodeStream(conn
                                .getInputStream());
                        handler.post(new Runnable() {

                            @Override
                            public void run() {
                                String tag = (String) iv.getTag();
                                if (tag != null && path.equals(tag)) {
                                    iv.setImageBitmap(bitmap);
                                }
                            }
                        });
                    }
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}
